package main

import (
	"fmt"
	"io/ioutil"
	"log"
	"net/http"
	"net/http/httputil"
	"net/url"
	"proxy"
	"strings"
)

func main() {

	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {

		targetURL, _ := url.Parse(r.URL.RequestURI())

		fmt.Println(r.URL)

		reverseProxy := httputil.NewSingleHostReverseProxy(r.URL)

		context, _ := ioutil.ReadAll(r.Body)

		reqContent := string(context)

		r.Body = ioutil.NopCloser(strings.NewReader(reqContent))

		fmt.Println(targetURL.RequestURI())
		fmt.Println(reqContent)

		rw := proxy.NewResponseWriter(w)

		reverseProxy.ServeHTTP(rw, r)

		fmt.Println(string(rw.Context))

		// TODO 匹配命令内容，调用TeleInfo API接口同步命令

	})

	fmt.Println("start")
	log.Fatal(http.ListenAndServe(":8888", nil))
}
